/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author rantipin
 */
public class PersistenceHelper {
    
    private final static String JDBC_PREFEX = "jdbc:mysql:";
    
    private PersistenceHelper() {   }
    
    public void testConnection(String login, String password, String connectionURL) throws SQLException {
        Connection conn = null;
        try {
            System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(JDBC_PREFEX + connectionURL, login, password);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    }
    
    public void initializeEntityManager(String login, String password, String connectionURL) {
        props = new Properties();           
        props.setProperty ("hibernate.connection.username", login);
        props.setProperty ("hibernate.connection.password", password);
        props.setProperty ("hibernate.connection.url", JDBC_PREFEX + connectionURL);     
    }
    
    private Properties props = null;
    
    public static PersistenceHelper getInstance() {
        return SingletonHolder.INSTANCE;
    } 
    
    private static class SingletonHolder {
        static final PersistenceHelper INSTANCE = new PersistenceHelper();
    }
    
    private void checkEntityManager() {
        if (factory == null) {
            factory = Persistence.createEntityManagerFactory("VideoControlPU", props);
            manager = factory.createEntityManager();
        }
    }

    public ArrayList<Research> findReseachesByDate(Date startDate, Date endDate) {
        checkEntityManager();

        ArrayList<Research> researches = new ArrayList<Research>();
        try {
            Query query = manager.createQuery("SELECT r FROM Research r WHERE r.date >= :start AND r.date <= :end");
            query.setParameter("start", startDate);
            query.setParameter("end", endDate);
            researches = new ArrayList<Research>(query.getResultList());

        } finally {
            /*if (manager != null) {
                manager.close();
                manager = null;
            }
            if (factory != null) {
                factory.close();
                factory = null;
            }*/
        }
        return researches;
    }

    public void saveAdchesion(Sample sample, String name, Double blackPercent, String description) {
        checkEntityManager();

        Photo photo = new Photo();
        photo.setName(name);
        photo.setPercentBlack(blackPercent);
        photo.setDescription(description);
        photo.setIdSample(sample);

        manager.getTransaction().begin();
        manager.persist(photo);
        manager.getTransaction().commit();
    }

    private EntityManagerFactory factory = null;
    private EntityManager manager = null;
}

 
